﻿//----------------------------------------------------

#region 命名空间

//    版权：
//    作者：
//建立时间：2012-09-11 00:00:33
//    描述：
//----------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI.WebControls;
using WinnerCMS.BLL.PageFactory;
using WinnerCMS.Common;
using WinnerCMS.Data;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.BLL
{
    public partial class GoodsManage
    {
        private static readonly GoodsDal MyDAL = new GoodsDal();

        #region 显示产品

        /// <summary>
        /// 显示产品
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public Goods PublicShow(int ID)
        {
            return MyDAL.PublicShow(ID);
        }

        #endregion

        #region 热门产品

        /// <summary>
        /// 热门产品
        /// </summary>
        /// <param name="TopNo"></param>
        /// <returns></returns>
        public IList<Goods> HotGoods(int TopNo, int CategoryId)
        {
            IList<Goods> list = MyDAL.HotGoods(TopNo, CategoryId);
            for (int i = 0; i < list.Count; i++)
            {
                list[i].Link = StaticFilePath.ContentShowUrl(list[i]);
            }
            return list;
        }

        #endregion

        #region 推荐产品

        /// <summary>
        /// 推荐产品
        /// </summary>
        /// <param name="TopNo"></param>
        /// <returns></returns>
        public IList<Goods> EliteGoods(int TopNo)
        {
            IList<Goods> list = MyDAL.EliteGoods(TopNo);
            foreach (Goods t in list)
            {
                t.Link = StaticFilePath.ContentShowUrl(t);
            }
            return list;
        }

        #endregion

        #region 图文列表

        /// <summary>
        /// 图文列表
        /// </summary>
        /// <param name="TopNo"></param>
        /// <returns></returns>
        public IList<Goods> PicGoods(int TopNo)
        {
            IList<Goods> list = MyDAL.PicGoods(TopNo);
            for (int i = 0; i < list.Count; i++)
            {
                list[i].Link = StaticFilePath.ContentShowUrl(list[i]);
            }
            return list;
        }

        #endregion

        #region 加载订单

        /// <summary>
        /// 加载订单
        /// </summary>
        /// <param name="GoodsID"></param>
        /// <returns></returns>
        public IList<ShoppingCart> ShowBuyUI(int GoodsID)
        {
            return MyDAL.ShowBuyUI(GoodsID, UserManage.Current.UserName);
        }

        #endregion

        /// <summary>
        /// 特价商品
        /// </summary>
        /// <param name="TopNo"></param>
        /// <returns></returns>
        public IList<Goods> GetTeJia(int TopNo)
        {
            IList<Goods> list = MyDAL.GetTeJia(TopNo);
            for (int i = 0; i < list.Count; i++)
            {
                list[i].Link = StaticFilePath.ContentShowUrl(list[i]);
            }
            return list;
        }

        /// <summary>
        /// 读取阅读过的商品
        /// </summary>
        /// <param name="IDs"></param>
        /// <returns></returns>
        public IList<Goods> ReadedGoods(int TopNo, string[] IDs)
        {
            if (IDs.Length == 0)
            {
                return new List<Goods>();
            }
            IList<Goods> list = MyDAL.ReadedGoods(TopNo, MyConvert.ConvertToArr(IDs));
            for (int i = 0; i < list.Count; i++)
            {
                list[i].Link = StaticFilePath.ContentShowUrl(list[i]);
            }
            return list;
        }

        /// <summary>
        /// 读取商品信息，返回json格式
        /// </summary>
        /// <param name="context"></param>
        public void ReadGoodsInfo(HttpContext context)
        {
            int iGoodsID;

            if (int.TryParse(context.Request["ID"], out iGoodsID) == false)
            {
                return;
            }

            Goods model = MyDAL.GetInfo(iGoodsID);
            if (model == null) return;

            StringBuilder json = new StringBuilder();
            json.AppendLine("{");
            json.AppendLine("\"ContentTitle\": \"" + model.ContentTitle + " \",");
            json.AppendLine("\"PicUrl\": \"" + model.PicUrl + " \",");
            json.AppendLine("\"Content\": \"" + model.Brief + " \"");
            json.AppendLine("}");

            context.Response.Write(json.ToString());
        }

        /// <summary>
        /// 绑定分页
        /// </summary>
        /// <param name="BrandID"></param>
        /// <param name="CategoryIds"></param>
        /// <param name="Sort"></param>
        /// <param name="OrderBy"></param>
        /// <param name="pp"></param>
        /// <returns></returns>
        public IList<Goods> BindList(string BrandID, int CategoryId, string Sort, string OrderBy, PagingParam pp)
        {
            IEnumerable<int> CategoryIds = CategoryManage.GetDescendantIds(CategoryId);

            int? _BrandID;
            if (BrandID == null || BrandID == "0")
            {
                _BrandID = null;
            }
            else
            {
                _BrandID = int.Parse(BrandID);
            }
            return MyDAL.BindList(_BrandID, CategoryIds, Sort, OrderBy, pp);
        }
    }
}